//
//  LJPreIssueView1.m
//  LajinLive
//
//  Created by 刘洋 on 16/9/18.
//  Copyright © 2016年 lajin. All rights reserved.
//

#import "IGPreIssueView.h"
#import "IGPermissionUtil.h"

#define kRadianToDegrees(radian) (radian* 180.0 )/(M_PI)

#define kShowAnimatinDuration1 0.3
#define kShowAnimatinDuration2 0.15
#define kHiddenAnimatinDuration2 0.45
#define kSpaceTime 0.05

typedef void(^didSelectOneType)(int);

@interface IGPreIssueView()

@property (nonatomic, strong) UIControl *controlSphoto;
@property (nonatomic, strong) UIControl *controlPphoto;
@property (nonatomic, strong) UIControl *controlTopic;
@property (nonatomic, strong) UIVisualEffectView *bgView;

@property (nonatomic, strong) UIButton *sPhotoBtn;
@property (nonatomic, strong) UIButton *pPhotoBtn;
@property (nonatomic, strong) UIButton *topicBtn;
@property (nonatomic, strong) UILabel *sPhotoLabel;
@property (nonatomic, strong) UILabel *pPhotoLabel;
@property (nonatomic, strong) UILabel *topicLabel;

@property (nonatomic, strong) UIButton *closeBtn;

@end

@implementation IGPreIssueView
{
    int angle;
    // 结束动画完成  == 2 的时候
    int finishCount;
    
    // 生气动画完成  == 2 的时候
    int showCount;
    
    didSelectOneType callback;
    float _maxMoveDistance;
}

- (instancetype)init
{
    self = [super initWithFrame:kKeyWindow.bounds];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES;
        [self createUI];

        _maxMoveDistance = 200;
        [self addGesture];
    }
    return self;
}

- (void)createUI
{
    [self addSubview:self.bgView];
    [self addSubview:self.controlSphoto];
    [self addSubview:self.controlPphoto];
    [self addSubview:self.controlTopic];
    
    [self addSubview:self.closeBtn];
    
    [_controlSphoto addSubview:self.sPhotoBtn];
    [_controlSphoto addSubview:self.sPhotoLabel];
    [_controlPphoto addSubview:self.pPhotoBtn];
    [_controlPphoto addSubview:self.pPhotoLabel];
    [_controlTopic addSubview:self.topicBtn];
    [_controlTopic addSubview:self.topicLabel];
    
    [self addContraint];
}

- (UIVisualEffectView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIVisualEffectView alloc] init];
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _bgView.userInteractionEnabled = YES;
        _bgView.effect = effect;
    }
    return _bgView;
}

- (UIControl *)controlSphoto
{
    if (!_controlSphoto) {
        _controlSphoto = [[UIControl alloc] init];
        [_controlSphoto addTarget:self action:@selector(controlDidClick:) forControlEvents:UIControlEventTouchUpInside];
//        _controlSphoto.backgroundColor = [UIColor greenColor];
        _controlSphoto.userInteractionEnabled = YES;
        _controlSphoto.tag = 100;
    }
    return _controlSphoto;
}

- (UIControl *)controlPphoto
{
    if (!_controlPphoto) {
        _controlPphoto = [[UIControl alloc] init];
        [_controlPphoto addTarget:self action:@selector(controlDidClick:) forControlEvents:UIControlEventTouchUpInside];
        _controlPphoto.userInteractionEnabled = YES;
        _controlPphoto.tag = 101;
    }
    return _controlPphoto;
}

- (UIControl *)controlTopic
{
    if (!_controlTopic) {
        _controlTopic = [[UIControl alloc] init];
        [_controlTopic addTarget:self action:@selector(controlDidClick:) forControlEvents:UIControlEventTouchUpInside];
        _controlTopic.userInteractionEnabled = YES;
        _controlTopic.tag = 102;
    }
    return _controlTopic;
}

- (UIButton *)closeBtn
{
    if (!_closeBtn) {
        _closeBtn = [[UIButton alloc] init];
        [_closeBtn setBackgroundImage:[UIImage imageNamed:@"pub_selected"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _closeBtn.alpha = 0.0;
    }
    return _closeBtn;
}

- (UIButton *)pPhotoBtn
{
    if (!_pPhotoBtn) {
        _pPhotoBtn = [[UIButton alloc] init];
        _pPhotoBtn.userInteractionEnabled = NO;
//        [_pPhotoBtn setBackgroundImage:[UIImage imageNamed:@"issue_pphoto_icon"] forState:UIControlStateNormal];
        [_pPhotoBtn setBackgroundImage:[UIImage imageNamed:NSLocalizedString(@"Artist",nil)] forState:UIControlStateNormal];
    }
    return _pPhotoBtn;
}

- (UIButton *)sPhotoBtn
{
    if (!_sPhotoBtn) {
        _sPhotoBtn = [[UIButton alloc] init];
        _sPhotoBtn.userInteractionEnabled = NO;
        [_sPhotoBtn setBackgroundImage:[UIImage imageNamed:@"issue_sphoto_icon"] forState:UIControlStateNormal];
    }
    return _sPhotoBtn;
}

- (UIButton *)topicBtn
{
    if (!_topicBtn) {
        _topicBtn = [[UIButton alloc] init];
        _topicBtn.userInteractionEnabled = NO;
        [_topicBtn setBackgroundImage:[UIImage imageNamed:@"issue_topic_icon"] forState:UIControlStateNormal];
    }
    return _topicBtn;
}

- (UILabel *)sPhotoLabel
{
    if (!_sPhotoLabel) {
        _sPhotoLabel = [[UILabel alloc] init];
//        _sPhotoLabel.text = @"私密作品";
        _sPhotoLabel.text = NSLocalizedString(@"Private Works", nil);
        _sPhotoLabel.numberOfLines = 0;
        _sPhotoLabel.font = [UIFont font10];
        _sPhotoLabel.textAlignment = NSTextAlignmentCenter;
        _sPhotoLabel.textColor = [UIColor blackColor];
    }
    return _sPhotoLabel;
}

- (UILabel *)topicLabel
{
    if (!_topicLabel) {
        _topicLabel = [[UILabel alloc] init];
        _topicLabel.text = NSLocalizedString(@"Topic", nil);
        _topicLabel.font = [UIFont font10];
        _topicLabel.textAlignment = NSTextAlignmentCenter;
        _topicLabel.textColor = [UIColor blackColor];
    }
    return _topicLabel;
}

- (UILabel *)pPhotoLabel
{
    if (!_pPhotoLabel) {
        _pPhotoLabel = [[UILabel alloc] init];
        _pPhotoLabel.text = NSLocalizedString(@"Public Works", nil);
        _pPhotoLabel.numberOfLines = 0;
        _pPhotoLabel.font = [UIFont font10];
        _pPhotoLabel.textAlignment = NSTextAlignmentCenter;
        _pPhotoLabel.textColor = [UIColor blackColor];
    }
    return _pPhotoLabel;
}

- (void)addContraint
{
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.and.bottom.mas_equalTo(0);
    }];
    
    CGFloat width = 60;
//    CGFloat height = 85;
    CGFloat height = 105;
    [_controlSphoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_bottom);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    
    [_controlPphoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(width, height));
        make.right.mas_equalTo(_controlSphoto.mas_left).mas_offset(-40);
    }];
    
    [_controlTopic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(width, height));
        make.left.mas_equalTo(_controlSphoto.mas_right).mas_offset(40);
    }];
    
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom).mas_offset(-7);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(55, 43));
    }];
    
    [_sPhotoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
//        make.height.mas_equalTo(height - 25);
        make.height.mas_equalTo(height - 45);
    }];
    
    [_sPhotoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
//        make.height.mas_equalTo(20);
        make.height.mas_equalTo(40);
    }];
    
    [_pPhotoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
//        make.height.mas_equalTo(height - 25);
        make.height.mas_equalTo(height - 45);
    }];
    
    [_pPhotoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
//        make.height.mas_equalTo(20);
        make.height.mas_equalTo(40);
    }];
    
    [_topicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
//        make.height.mas_equalTo(height - 25);
        make.height.mas_equalTo(height - 45);
    }];
    
    [_topicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
//        make.height.mas_equalTo(20);
        make.height.mas_equalTo(40);
    }];
}

- (void)addGesture
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeBtnClick)];
    [self.bgView addGestureRecognizer:tap];
}

- (void)closeBtnClick
{
    [self dismiss];
}

- (void)controlDidClick:(UIControl *)sender
{
    if (self.isCenterItemClickBlock) {
        self.isCenterItemClickBlock();
    }
    NSInteger index = sender.tag;
    if (index == 100) {
        [self removeFromSuperview];
        if (self.sPhotoControlBlock) {
            self.sPhotoControlBlock();
        }
    } else if (index == 101) {
        [self removeFromSuperview];
        if (self.pPhotoControlBlock) {
            self.pPhotoControlBlock();
        }
    } else if (index == 102) {
        [self removeFromSuperview];
        if (self.topicControlBlock) {
            self.topicControlBlock();
        }
    }
}

#pragma mark - public Method
- (void)show
{
    [self addBtnAnimation:YES];
    [self btnStatusChange:NO];
}

- (void)dismiss
{
    [self addBtnAnimation:NO];
    [self btnStatusChange:YES];
}

#pragma mark - 动画
- (void)showAnimation:(UIView *)view
{
    [UIView animateWithDuration:kShowAnimatinDuration1 animations:^{
        CGRect f = view.frame;
        f.origin.y -= _maxMoveDistance;
        view.frame = f;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:kShowAnimatinDuration2 animations:^{
            CGRect f = view.frame;
            f.origin.y += 20;
            view.frame = f;
        } completion:^(BOOL finished) {
            showCount++;
            if (showCount == 2) {
                showCount = 0;
            }
        }];
    }];
}

- (void)hiddenAnimation:(UIView *)view
{
    [UIView animateWithDuration:kShowAnimatinDuration1 + kShowAnimatinDuration2 animations:^{
        CGRect f = view.frame;
        f.origin.y += (_maxMoveDistance - 20);
        view.frame = f;
    } completion:^(BOOL finished) {
        finishCount ++;
        if (finishCount == 2) {
            [self removeFromSuperview];
            if (self.isCenterItemClickBlock) {
                self.isCenterItemClickBlock();
            }
            finishCount = 0;
        }
    }];
}
#define BKTimeDelay(t) dispatch_time(DISPATCH_TIME_NOW, (uint64_t)(NSEC_PER_SEC * t))
- (void)btnStatusChange:(BOOL)btnStaus
{
    if (btnStaus) {
        dispatch_after(BKTimeDelay(kSpaceTime * 1), dispatch_get_main_queue(), ^{
            [self hiddenAnimation:self.controlPphoto];
        });
        
        dispatch_after(BKTimeDelay(kSpaceTime * 2), dispatch_get_main_queue(), ^{
            [self hiddenAnimation:self.controlSphoto];
        });
        
        dispatch_after(BKTimeDelay(kSpaceTime * 3), dispatch_get_main_queue(), ^{
            [self hiddenAnimation:self.controlTopic];
        });
    } else {
        self.hidden = NO;
        
        dispatch_after(BKTimeDelay(kSpaceTime * 1), dispatch_get_main_queue(), ^{
            [self showAnimation:self.controlPphoto];
        });
        
        dispatch_after(BKTimeDelay(kSpaceTime * 2), dispatch_get_main_queue(), ^{
            [self showAnimation:self.controlSphoto];
        });
        
        dispatch_after(BKTimeDelay(kSpaceTime * 3), dispatch_get_main_queue(), ^{
            [self showAnimation:self.controlTopic];
        });
    }
}

- (void)addBtnAnimation:(BOOL)flag
{
//    angle += 135;
//    if (angle % 360 == 0) {
//        angle = 0;
//    }
//    CGAffineTransform endAngle = CGAffineTransformMakeRotation(angle * (M_PI / 270.0f));
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//        _closeBtn.transform = endAngle;
        if (flag) {
            _closeBtn.alpha = 1.0;
        } else {
            _closeBtn.alpha = 0.0;
        }
    } completion:^(BOOL finished) {
    }];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
